package com.woniu.service.impl;

import com.woniu.entity.Menu;
import com.woniu.mapper.MenuMapper;
import com.woniu.service.MenuService;
import com.woniu.util.ResponseEnum;
import com.woniu.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenuMapper mapper;
    @Override
    public ResponseUtil findMenuById(Integer userId) {
        List<Menu> menuList = mapper.findMenu(userId);
        //获取所有的父菜单
        List<Menu> parentMenuList = menuList.stream()
                .filter(e -> e.getParentId().equals(-1))
                .collect(Collectors.toList());
        //遍历所有的父菜单
        for (Menu parentMenu : parentMenuList) {
            List<Menu> list = new ArrayList<>();
            for (Menu menu : menuList) {
                //判断子菜单的parentId是否与父菜单的id相等
                if (menu.getParentId().equals(parentMenu.getId())){
                    list.add(menu);
                }
            }
            //将子菜单放入父菜单中
            parentMenu.setSubMenuList(list);
        }
        return ResponseUtil.get(ResponseEnum.OK,parentMenuList);
    }
}
